In [1]:
%reload_ext autoreload
%autoreload 2
%matplotlib notebook
import sys
sys.path.append('..')
from helper import nn
from helper import logistic_regression as lr
import numpy as np
In [2]:
X_raw, y_raw = nn.load_data('ex4data1.mat', transpose=False)
X = np.insert(X_raw, 0, np.ones(X_raw.shape[0]), axis=1)
X.shape
Out[2]:
In [3]:
y = nn.expand_y(y_raw)
y.shape
Out[3]:
In [4]:
t1, t2 = nn.load_weight('ex4weights.mat')
t1.shape, t2.shape
Out[4]:
In [5]:
theta = nn.serialize(t1, t2) # flatten params
theta.shape
Out[5]:
In [6]:
nn.sigmoid_gradient(0)
Out[6]:
In [7]:
d1, d2 = nn.deserialize(nn.gradient(theta, X, y))
In [8]:
d1.shape, d2.shape
Out[8]:
In [9]:
# nn.gradient_checking(theta, X, y, epsilon= 0.0001)
In [10]:
# nn.gradient_checking(theta, X, y, epsilon=0.0001, regularized=True)
remember to randomly initlized the parameters to break symmetry
take a look at the doc of this argument: jac
jac : bool or callable, optional Jacobian (gradient) of objective function. Only for CG, BFGS, Newton-CG, L-BFGS-B, TNC, SLSQP, dogleg, trust-ncg. If jac is a Boolean and is True, fun is assumed to return the gradient along with the objective function. If False, the gradient will be estimated numerically. jac can also be a callable returning the gradient of the objective. In this case, it must accept the same arguments as fun.
it means if your backprop
function return (cost, grad)
, you could set jac=True
This is the implementation of http://nbviewer.jupyter.org/github/jdwittenauer/ipython-notebooks/blob/master/notebooks/ml/ML-Exercise4.ipynb
but I choose to seperate them
In [11]:
res = nn.nn_training(X, y)
res
Out[11]:
In [12]:
_, y_answer = nn.load_data('ex4data1.mat')
y_answer[:20]
Out[12]:
In [13]:
final_theta = res.x
In [16]:
nn.show_accuracy(final_theta, X, y_answer)
In [15]:
nn.plot_hidden_layer(final_theta)
In [ ]: